package com.tianjian.common.filter;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.tianjian.common.Config;
import com.tianjian.common.logging.Logging;
import com.tianjian.oa.system.pojos.UserInfo;

@SuppressWarnings("unused")
public class AuthorityInterceptor extends MethodFilterInterceptor {

	private Logger logger = Logger.getLogger(AuthorityInterceptor.class);

	/** 日志记录 */
	private Logging logging;

	private static final long serialVersionUID = -4080450860505746910L;

	private static final String NOROLE = "norole";

	private static final String EXCEPTION = "exception";

	private static String[] GLOBAL_PATH = null;

	private static String[] GNORE_PATH = null;

	@Override
	protected String doIntercept(ActionInvocation actioninvocation) throws Exception {
		String result = null;
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		UserInfo user = (UserInfo) session.getAttribute(Config.SESSION_USERINFO);
		String path = request.getServletPath(); // 获取当前访问路劲
		logger.info(new StringBuilder().append("\t===[[ ").append((user!=null?user.getUsername():"null")).append(" - ").append(path).append(" ]]===").toString());
		if(user != null){
//			if (user.getMenus() == null || !checkPath(user.getMenus(), path)) {
//				return NOROLE; // 没有权限访问页面
//			}
			session.setMaxInactiveInterval(Config.SESSION_TIMEOUT);
			long start = System.currentTimeMillis();
			Throwable ex = null;
			try {
				result = actioninvocation.invoke();
			} catch (Exception e) {
				ex = e;
				e.printStackTrace();
//				throw new RuntimeException(e);
				result = EXCEPTION;
			}
			long spend = System.currentTimeMillis() - start;
			if (ex != null || !isIgnore(path)) {
				logging.log(request, actioninvocation.getAction(), user, spend, ex);
			}
		} else {
			result = Action.LOGIN; // 没有登陆
		}
		System.gc();
		return result;
	}

	private boolean isIgnore(String path) {
		gnorePath();
		for (int i = 0; i < GNORE_PATH.length; i++) {
			if (path.equals(GNORE_PATH[i])) {
				return true;
			}
		}
		return false;
	}

	private void gnorePath() {
		if (GNORE_PATH == null) {
			try {
				Properties properties = new Properties();
				String classpath = Thread.currentThread().getContextClassLoader().getResource("/").toString();  
				classpath = classpath.substring(5);
				properties.load(new BufferedInputStream(new FileInputStream(classpath + "/global.properties")));
				String path = properties.getProperty("ignorePath", "");
				GNORE_PATH = path.split(",");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 设置[日志记录]
	 * @param logging
	 */
	public void setLogging(Logging logging) {
		this.logging = logging;
	}

}
